package com.tourism.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.stereotype.Repository;

import com.tourism.model.UserInfo;
import com.tourism.shared.SysUtils;


@Repository
public class UserInfoDao {
	
	@SuppressWarnings("resource")
	public UserInfo selectUserForLogin(String name)
			throws SQLException {
		PreparedStatement pstmt = null;
		Connection con = SysUtils.getMainDBCon();
		StringBuffer sql = new StringBuffer();
		ResultSet rs = null;
		UserInfo user = new UserInfo();
		sql.append("select * from  user ");
		sql.append("  where name = " + name);
		try {
			pstmt = con.prepareStatement(sql.toString());
			rs = pstmt.executeQuery();
			if (rs.next()) {
				user.setUserId(rs.getString("UserId"));
				user.setPassword(rs.getString("Password"));
				user.setName(rs.getString("Name"));
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			SysUtils.colseDBCon(con, pstmt, null);
		}
		return user;
		
	}
	
	public void insertUser(String userID, String password,String phoneNumber) throws SQLException{
		PreparedStatement pstmt = null;
		Connection con = SysUtils.getMainDBCon();
		StringBuffer sql = new StringBuffer();

		sql.append("INSERT INTO user ( ");
		sql.append("  name,password,phone");
		sql.append("  ) VALUES (?,?,?) ");

		try {
			pstmt = con.prepareStatement(sql.toString());
			pstmt.setString(1, userID);
			pstmt.setString(2, password);
			pstmt.setString(3, phoneNumber);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			SysUtils.colseDBCon(con, pstmt, null);
		}
	}

}
